package leetcode.剑指offer.后40;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author :songyaozhang
 * @date :Created 2021/8/10 19:38
 * @description :https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/
 */
public class 剑指_57_II_和为s的连续正数序列 {
    @Test
    public void test() {
        Solution solution = new Solution();
        solution.findContinuousSequence(15);
    }

    class Solution {
        public int[][] findContinuousSequence(int target) {
            int l = 1;
            int r = l;
            int sum = 0;
            List<int[]> ans = new ArrayList<>();
            ArrayList<Integer> list = new ArrayList<>();
            while (r <= target) {
                if (sum == target) {
                    int[] nums = new int[list.size()];
                    for (int i = 0; i < list.size(); i++) {
                        nums[i] = list.get(i);
                    }
                    ans.add(nums);
                    list = new ArrayList<>(list);
                }
                if (sum <= target) {
                    sum += r;
                    list.add(r);
                    r++;
                } else {
                    sum -= l;
                    list.remove(0);
                    l++;
                }
            }
            return ans.toArray(new int[ans.size()][]);
        }
    }
}
